package com.mars.module.admin.controller;


import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import com.mars.common.enums.BusinessType;
import com.mars.common.result.R;
import com.mars.common.util.ExcelUtils;
import com.mars.module.admin.response.SysDeptResponse;
import io.swagger.annotations.Api;
import com.mars.framework.annotation.RateLimiter;
import com.mars.module.admin.entity.SysDept;
import com.mars.framework.annotation.Log;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import com.mars.common.response.PageInfo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.service.ISysDeptService;
import com.mars.module.admin.request.SysDeptRequest;

import javax.servlet.http.HttpServletResponse;

/**
 * 部门控制层
 *
 * @author mars
 * @date 2024-04-02
 */
@Slf4j
@AllArgsConstructor
@RestController
@Api(value = "部门接口管理",tags = "部门接口管理")
@RequestMapping("/admin/sysDept" )
public class SysDeptController {

    private final ISysDeptService iSysDeptService;

    @PostMapping("/list")
    @ApiOperation(value = "获取菜单树")
    public R<List<SysDeptResponse>> list(@Validated @RequestBody SysDeptRequest queryDto) {
        return R.success(iSysDeptService.tree(queryDto));
    }

    /**
     * 分页查询部门列表
     */
    @ApiOperation(value = "分页查询部门列表")
    @PostMapping("/pageList")
    public R<PageInfo<SysDept>> pageList(@RequestBody SysDeptRequest sysDeptRequest) {
        return R.success(iSysDeptService.pageList(sysDeptRequest));
    }

    /**
     * 获取部门详细信息
     */
    @ApiOperation(value = "获取部门详细信息")
    @GetMapping(value = "/query/{id}")
    public R<SysDept> detail(@PathVariable("id") Long id) {
        return R.success(iSysDeptService.getById(id));
    }

    /**
     * 新增部门
     */
    @Log(title = "新增部门", businessType = BusinessType.INSERT)
    @RateLimiter
    @ApiOperation(value = "新增部门")
    @PostMapping("/add")
    public R<Void> add(@RequestBody SysDeptRequest sysDeptRequest) {
        iSysDeptService.add(sysDeptRequest);
        return R.success();
    }

    /**
     * 修改部门
     */
    @Log(title = "修改部门", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改部门")
    @PostMapping("/update")
    public R<Void> edit(@RequestBody SysDeptRequest sysDeptRequest) {
        iSysDeptService.update(sysDeptRequest);
        return R.success();
    }

    /**
     * 删除部门
     */
    @Log(title = "删除部门", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除部门")
    @PostMapping("/delete/{ids}")
    public R<Void> remove(@PathVariable Long[] ids) {
        iSysDeptService.deleteBatch(Arrays.asList(ids));
        return R.success();
    }

    /**
    * 导出部门
    *
    * @param response response
    * @throws IOException IOException
    */
    @PostMapping(value = "/export")
    public void exportExcel(HttpServletResponse response,@RequestBody SysDeptRequest sysDeptRequest) throws IOException {
        List<SysDept> list = iSysDeptService.list(sysDeptRequest);
        ExcelUtils.exportExcel(list, SysDept.class, "部门信息", response);
    }

    @GetMapping("/get/{id}")
    @ApiOperation(value = "获取详情")
    public R<SysDeptResponse> get(@PathVariable("id") Long id) {
        return R.success(iSysDeptService.get(id));
    }

}
